 
 package bus.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;

import bus.dao.TicketDao;
import bus.dao.UserDao;
import bus.dao.cityDao;
import bus.moudle.Admin;
import bus.moudle.User;
import bus.util.DButil;
	
  public class BookTickets extends JPanel implements ActionListener
   {
	   String cclass[]={"1st","2nd","3rd"};
	    
	   
	  JLabel name,Class,age,seats,gender,email,dob,point,destination,number,address,text1,text2;
	  
	   JTextField tname,tage,tseats,tgender,temail,tdob,tnumber,taddress;
	   
	   JComboBox tclass,tpoint,tdestination;
	   
	   JButton choice,date;
	    
	   JFrame jf;
	   
	  BookTickets(String username) throws Exception { 
	    DButil dbUtil=new DButil();
		Connection con=dbUtil.getCon();
		User user=new User();
		user.setUser_name(username);
		UserDao k=new UserDao();
		ResultSet rs= k.login(con,user);
		
	   setLayout(null);
	   setBounds(100,100,800,800);
	   name=new JLabel("Full Name:");
	   name.setBounds(50,80,100,30);
	   name.setFont(new Font("Arial", Font.PLAIN, 15));
	   add(name);
	   
	   tnumber=new JTextField();
	   number=new JLabel("Contact Number:");
	    tage=new JTextField();
	    tname=new JTextField();
	   if(rs.next()) {  
		  System.out.println(rs.getString("gender"));
	   
	   tname.setText(username);
	   tname.setBounds(160,80,250,30);
	   tname.setFont(new Font("Arial", Font.PLAIN, 15));
       tname.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
	   tname.setBackground(Color.yellow);
	   
	   Class=new JLabel("Class:");
	   Class.setBounds(450,80,100,30);
	   Class.setFont(new Font("Arial", Font.PLAIN, 15));
	   
       tclass=new JComboBox(cclass);
		tclass.setBounds(590, 80, 250, 30);
		tclass.setFont(new Font("Arial", Font.PLAIN, 15));
		 tclass.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
		   tclass.setBackground(Color.yellow);
		
	   age=new JLabel("Age:");
	   age.setBounds(50,130,100,30);
	   age.setFont(new Font("Arial", Font.PLAIN, 15));
	   add(age);
	   
	  
	   tage.setText(rs.getString("age"));
	   tage.setBounds(160,130,250,30);
	   tage.setFont(new Font("Arial", Font.PLAIN, 15));
       tage.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
	   tage.setBackground(Color.yellow);
	   
	   seats=new JLabel("Seats:");
	   seats.setBounds(450,130,100,30);
	   seats.setFont(new Font("Arial", Font.PLAIN, 15));
	   tseats=new JTextField();
	   tseats.setBounds(590,130,250,30);
	   tseats.setFont(new Font("Arial", Font.PLAIN, 15));
       tseats.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
	   tseats.setBackground(Color.yellow);
	   
	   gender=new JLabel("Gender:");
	   gender.setBounds(50,180,100,30);
	   gender.setFont(new Font("Arial", Font.PLAIN, 15));
	   
	   tgender=new JTextField(rs.getString("gender"));
	   tgender.setBounds(160,180,250,30);
	   tgender.setFont(new Font("Arial", Font.PLAIN, 15));
       tgender.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
	   tgender.setBackground(Color.yellow);
	   
	   
	   number.setBounds(20,230,120,30);
	   number.setFont(new Font("Arial", Font.PLAIN, 15));
    	
	  
	   tnumber.setText(rs.getString("contact"));
	   tnumber.setBounds(160,230,250,30);
	   tnumber.setFont(new Font("Arial", Font.PLAIN, 15));
	   tnumber.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
	   tnumber.setBackground(Color.yellow);
	
	   }
	   
	   add(tnumber);
	   add(number);
	   add(tgender);
	   add(gender);
	   add(tseats);
	   add(seats);
	   add(name);
	   add(tage);add(tclass); add(Class); add(tname);
	   
	   point=new JLabel("Pickup Point:");
	   point.setBounds(450,230,100,30);
	   point.setFont(new Font("Arial", Font.PLAIN, 15));
	   add(point);
	  
       tpoint=new JComboBox();
		tpoint.setBounds(590, 230, 250, 30);
		tpoint.setFont(new Font("Arial", Font.PLAIN, 15));
		 tpoint.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
		   tpoint.setBackground(Color.yellow);
		add(tpoint);
		
	       dob=new JLabel("DOB:");
			dob.setBounds(50, 280, 100, 30);
			dob.setFont(new Font("Arial", Font.PLAIN, 15));
			add(dob);
		   tdob=new JTextField();
		   tdob.setBounds(160,280,140,30);
		   tdob.setFont(new Font("Arial", Font.PLAIN, 15));
			 tdob.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
			   tdob.setBackground(Color.yellow);
			add(tdob);
			date=new JButton("Choice");
			date.setBounds(310,280,100,30);
			date.setFont(new Font("Arial", Font.BOLD, 13));
			add(date);
			date.addActionListener(new ActionListener() 
			 {
			  public void actionPerformed(ActionEvent ae) 
			  {
			   //set text i.e. date
			   tdob.setText(new DatePicker(jf).setPickedDate());
			  }
			    });

		       destination=new JLabel("Destination Point:");
				destination.setBounds(450, 280, 130, 30);
				 destination.setFont(new Font("Arial", Font.PLAIN, 15));
				add(destination);
			  tdestination=new JComboBox();
				tdestination.setBounds(590, 280, 250, 30);
				tdestination.setFont(new Font("Arial", Font.PLAIN, 15));
				tdestination.addItem("");
				 tdestination.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0)));
				   tdestination.setBackground(Color.yellow);
				add(tdestination);
			

                text1=new JLabel("* the price of the 1st class is more than 120$ , the price of the 2nd class is more than 90$ and less than 120$");
                text1.setBounds(40,320,800,30);
                text1.setFont(new Font("Arial", Font.PLAIN, 13));
                text1.setBackground(Color.gray);
                add(text1);
                text2=new JLabel("the price of the 1st class is less than 90$");
                text2.setBounds(50,340,800,30);
                text2.setFont(new Font("Arial", Font.PLAIN, 13));
                text2.setBackground(Color.gray);
                add(text2);
				
				choice=new JButton("Book Now");
				choice.setBounds(400,400,130,40);
				choice.setFont(new Font("Arial", Font.PLAIN, 15));
				add(choice);
				choice.addActionListener(this);
				try
		        {
		        DButil db=new DButil();
		        cityDao cd=new cityDao();
		         rs=cd.citySelect(con);
		      
		     while(rs.next()) {
		     tpoint.addItem(rs.getString("city_name"));
		     tdestination.addItem(rs.getString("city_name"));
		     
		    }
		        }
		  catch(Exception e)
		  {
			  e.printStackTrace();
		      System.out.println(e);
		  }
		  
				setLayout(new FlowLayout());
				setLayout(null);
				setBounds(400,600,800,900);
			 
			    
	  }
	   
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try { 
		String fullname=tname.getText();
		String age=tage.getText();
		String seat=tseats.getText();
		if(age.equals("")||seat.equals("")) {
			JOptionPane.showMessageDialog(this,"Please input all the information");
		}
		else {
		int seats=Integer.parseInt(seat);
		String gender=tgender.getText();
		String doj=tdob.getText();
		String contact=tnumber.getText();
		String Class=tclass.getSelectedItem().toString();
		String pickup_point=tpoint.getSelectedItem().toString();
		String destination_point=tdestination.getSelectedItem().toString();	
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	    java.util.Date t= formatter.parse(doj);
	    
			DButil dbUtil=new DButil();
			   Connection con=dbUtil.getCon();
			  cityDao CD=new cityDao();
			  
				Admin m=new Admin(fullname, t , pickup_point, destination_point, seats);
				
				User u=new User();
//				u.setAddress(address);
				u.setAge(age);
//				u.setEmailID(emailID);
				u.setGender(gender);
				u.setContact(contact);
				
				TicketDao td=new TicketDao();  				
		      if(td.choice(u, m)==true)
		      {
		    	  JOptionPane.showMessageDialog(this,"Please input all the information");
		      }
		      else {

			   // search.isSame(con,m);
				if(td.isSame(con,m)==true)
				{
					JOptionPane.showMessageDialog(this,"Pickup_point and Destination_point Can't same!");
				}
		
				else {

					TicketDao add=new TicketDao();
					ResultSet rs=td.ifbusExit(con, m);
					if(rs.next()){
					 String id=add.ticketAdd(con,m, rs.getString("bus_num"), rs.getString("Price"));
					 JOptionPane.showMessageDialog(this,"Your tickets number is: "+id+"\nThe price of your tickets is:"+rs.getString("Price")); 
				  }
				else {
						JOptionPane.showMessageDialog(this, "No bus need your mind.");
				}
		      }
		     }
		}
		}
		catch(Exception el) {
			el.printStackTrace();
			System.out.print(el);
		}
	}     
	}
  